#include <QMessageBox>
#include "startup.h"
#include "ui_startup.h"
#include <QTimer>
#include "selfcheckfail.h"
#include "commonfunction.h"
#include "log4z.h"
#include "systempreset.h"
#include "diskchecker.h"
#include "motorcalibdialog.h"

StartUp::StartUp(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::StartUp)
    , userSkipSelfCheck(false)
{
    ui->setupUi(this);
    bdf = BllDataFlow::getInstance();
    bCtrl = BllController::getInstance();
    //Signals and Slots
    connect(bCtrl, &BllController::progressChangeSignal, this, &StartUp::onProgressChanged);
    //self check
    connect(bCtrl, &BllController::selfCheckResultPassSignal, this, &StartUp::selfCheckPassSlot);
    connect(bCtrl, &BllController::selfCheckResultFailSignal, this, &StartUp::selfCheckFailSlot);
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &StartUp::retranslateUiSlot);
    connect(bdf, &BllDataFlow::buildFinishedSignal, this, &StartUp::setWidgetParent);

    initWidget();
    retranslateUiSlot();
}

StartUp::~StartUp()
{
    delete ui;
}

void StartUp::showEvent(QShowEvent *)
{
    this->activateWindow();
    bCtrl->startPost();
}

void StartUp::setWidgetParent()
{
    selfCheckFailDialog->setParent(Project::Instance());
}

void StartUp::onProgressChanged(int val)
{
    if (val >= 0 && val <= progressCount)
    {
        ui->progressBar->setValue(val);
        ui->progressNumber->setText(QString::number(val * 100.0 / progressCount, 'f', 0) + "%");
    }
}

void StartUp::on_pbtnOK_clicked()
{
    userSkipSelfCheck = true;
    bdf->changeMainPageIndex(MainPageIndex::LOGIN);
}

void StartUp::initWidget()
{
    if (windowWidth >= 1280) {
        this->setProperty("cssSize", "large");
    }

    progressCount = SystemPreset::instance()->getIntData(EnumPresetItem::SelfTestMaxTimes) * 1000;
    bool showStartupBtn = SystemPreset::instance()->getBoolData(EnumPresetItem::ShowStartupButton);
    ui->pbtnOK->setVisible(showStartupBtn);
    ui->progressBar->setRange(0, progressCount);
    ui->progressBar->setUpdatesEnabled(true);
    selfCheckFailDialog = new SelfCheckFail(this);
}

void StartUp::selfCheckPassSlot(const HostReply &reply)
{
    qDebug() << "StartUp::selfCheckPassSlot:" <<  reply.message << endl;
    LOGFMTD("selfcheck success: %s", reply.parameter.toString().toStdString().data());
    if (!userSkipSelfCheck)
    {
        this->hide();
        ui->progressBar->setValue(progressCount);
        bCtrl->setInstrumentStauts(Status::Idle);
        bdf->changeMainPageIndex(MainPageIndex::LOGIN);
        DiskChecker::checkFreeSpace(DRIVER_NAME);
    }
}

void StartUp::selfCheckFailSlot(const HostReply &reply)
{
    qDebug() << "StartUp::selfCheckFailSlot:" << reply.message << endl;
    LOGFMTD("selfcheck fail: %s", reply.parameter.toString().toStdString().data());
    bCtrl->setInstrumentStauts(Status::Error);
    if (!userSkipSelfCheck)
    {
        if (reply.parameter == "08")
        {
            MotorCalibDialog(Project::Instance()).show(true);
            return;
        }

        selfCheckFailDialog->show(reply.message);
    }
}

void StartUp::retranslateUiSlot()
{
    ui->retranslateUi(this);

    if (QString::compare(gLanguage, "CN", Qt::CaseInsensitive))
    {
        ui->lblImage->setStyleSheet("border-image: url(:/resources/title_en.png);");
    }
    else
    {
        ui->lblImage->setStyleSheet("border-image: url(:/resources/title_cn.png);");
    }
}
